package pers.xukai.common.utils.email;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pers.xukai.common.exception.BizException;

import java.util.List;

@Component
public class EmailUtils {
    /**
     * 邮件服务器主机地址
     */
    private static String host;

    /**
     * 邮件服务器主机端口
     */
    private static String port;

    /**
     * 发送邮件的邮箱地址
     */
    private static String from;

    /**
     * 发送邮箱的用户名
     */
    private static String user;

    /**
     * 邮箱授权码
     */
    private static String password;

    /**
     * 根据模板发送邮件
     * @param addressee 收件人
     * @param template 模板
     * @param subject 标题
     * @param context 内容插值
     * @return 是否发送成功
     */
    public static boolean sendTemplateEmail(List<String> addressee, EmailTemplate template, String subject, String... context) {
        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(Integer.parseInt(port));
        account.setAuth(true);
        account.setFrom(from);
        account.setUser(user);
        account.setPass(password);
        String templateContext = getContentByTemplate(template, context);
        try {
            MailUtil.send(account, addressee, subject, templateContext, true);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 自定义发送邮件
     * @param addressee 收件人
     * @param subject 标题
     * @param context 内容
     * @return 是否发送成功
     */
    public static boolean sendEmail(List<String> addressee, String subject, String context){
        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(Integer.parseInt(port));
        account.setAuth(true);
        account.setFrom(from);
        account.setUser(user);
        account.setPass(password);
        try {
            MailUtil.send(account, addressee, subject, context, true);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 自定义发送邮件
     * @param address 收件人
     * @param subject 标题
     * @param context 内容
     * @return 是否发送成功
     */
    public static boolean sendEmail(String address, String subject, String context){
        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(Integer.parseInt(port));
        account.setAuth(true);
        account.setFrom(from);
        account.setUser(user);
        account.setPass(password);
        List<String> addressee = ListUtil.of(address);
        try {
            MailUtil.send(account, addressee, subject, context, true);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }

    @Value("${k-full-mark.email.host}")
    private void setHost(String host) {
        EmailUtils.host = host;
    }

    @Value("${k-full-mark.email.port}")
    private void setPort(String port) {
        EmailUtils.port = port;
    }

    @Value("${k-full-mark.email.addr}")
    private void setFrom(String from) {
        EmailUtils.from = from;
    }

    @Value("${k-full-mark.email.user}")
    private void setUser(String user) {
        EmailUtils.user = user;
    }

    @Value("${k-full-mark.email.password}")
    private void setPassword(String password) {
        EmailUtils.password = password;
    }

    /**
     * 填充邮件模板并生成邮件内容
     * @param emailTemplate 邮件模板
     * @param interpolation 插值内容
     * @return String 邮件内容
     */
    private static String getContentByTemplate(EmailTemplate emailTemplate, String... interpolation){
        String templateContext = emailTemplate.getContext();
        int count = StringUtils.countMatches(templateContext, "${}");
        if (count != interpolation.length){
            throw new BizException("邮件模板与参数不匹配！模板需要的参数为"+count+"个，传递了"+interpolation.length+"个参数!");
        }
        for (String s : interpolation) {
            templateContext = templateContext.replaceFirst("\\$\\{}", s);
        }
        return templateContext;
    }
}
